{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "atlantic-staff",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/find-the-town-judge\n",
    "\n",
    "\n",
    "Runtime: 256 ms, faster than 52.50% of C++ online submissions for Find the Town Judge.\n",
    "Memory Usage: 77.2 MB, less than 6.34% of C++ online submissions for Find the Town Judge.\n",
    "\n",
    "\n",
    "```cpp\n",
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <iostream>\n",
    "#include <set>\n",
    "\n",
    "using namespace std;\n",
    "\n",
    "class Solution {\n",
    "public:\n",
    "    int findJudge(int N, vector<vector<int>>& trust) {\n",
    "        //7:22\n",
    "        if (trust.size() == 0 && N==1) {\n",
    "            return 1;\n",
    "        } \n",
    "        set<int> all_people;\n",
    "        set<int> not_judge;\n",
    "        set<int> trusted;\n",
    "        for(int i=0; i<=N; i++) {\n",
    "            all_people.insert(i);\n",
    "        }\n",
    "        for (auto l: trust) {\n",
    "            not_judge.insert(l[0]);\n",
    "            trusted.insert(l[1]);\n",
    "        }\n",
    "        set<int> temp1;\n",
    "        set<int> results;\n",
    "        set_difference(all_people.begin(), all_people.end(), not_judge.begin(), not_judge.end(), inserter(temp1, temp1.begin()));\n",
    "        set_intersection(temp1.begin(), temp1.end(), trusted.begin(), trusted.end(), inserter(results, results.begin()));\n",
    "        if (results.size() == 1) {\n",
    "            int possible =*results.begin();\n",
    "            set<int> make_sure;\n",
    "            for (auto l: trust) {\n",
    "                if (l[1] == possible) {\n",
    "                    make_sure.insert(l[0]);\n",
    "                }\n",
    "            }\n",
    "            if (make_sure == not_judge) {\n",
    "                return possible;\n",
    "            } else {\n",
    "                return -1;\n",
    "            }\n",
    "        } else {\n",
    "            return -1;\n",
    "        }\n",
    "        //7:40\n",
    "    }\n",
    "};\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "environmental-prize",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
